package Syntax_Anlayzer;

import java.io.*;
import java.util.*;

/*
 * 语法分析步骤第一步:
 * 读入LL1文法
 * 从LL1文法中得到终结符集合V_T、非终结符集合V_N
 * 得到对应的产生式Production
 * */

public class Init {
    //终结符集合
    public static Set<String> V_T = new HashSet<>();
    //非终结符集合
    public static Set<String> V_N = new HashSet<>();
    //产生式
    public static Map<String, Set<List<String>>> Production = new HashMap<>();

    //读取产生式行并分割左右两侧
    public void DivideToLeftRight(String str, List<String>left, List<String>right){
        String[] temp = str.split("->");

        String[] LeftArr = temp[0].split(" ");
        for (String a : LeftArr){
            if (!a.equals(" "))
                left.add(a);
        }

        String[] RightArr = temp[1].split(" ");
        for (String b : RightArr){
            if (!b.equals(" "))
                right.add(b);
        }
    }

    public void RemoveAFromB(Set A, Set B) {
        for (Object a : A) {
            B.remove(a);
        }
    }

    private static Init init = null;

    private Init(String file){
        try{
            BufferedReader readIn = new BufferedReader(new FileReader(file));
            String line;
            while(((line = readIn.readLine()) != null)){
                List<String> left = new ArrayList<>();
                List<String> right = new ArrayList<>();
                DivideToLeftRight(line, left, right);

                //左边是非终结符、右边作为列表添加到产生式
                if (!Production.containsKey(left.get(0))) Production.put(left.get(0), new HashSet<>());
                Production.get(left.get(0)).add(right);
                V_N.add(left.get(0));
                V_T.addAll(right);
                RemoveAFromB(V_N,V_T);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Init getInstance(String file) {
        if (init == null) {
            init = new Init(file);
        }
        return init;
    }

    public void convertFile(String file, String target){
        try{
            BufferedReader in = new BufferedReader(new FileReader(file));
            BufferedWriter out = new BufferedWriter(new FileWriter(target));
            String line;

            while(((line = in.readLine()) != null)){
                String[] str = line.split(" ");
                String[] leftArr = str[0].split(" ");
                String left = null;
                for (String a : leftArr)
                    if (!a.equals("")) left = a;
                List<String> Right = new ArrayList<>();
                String[] rightArr = str[1].split(" ");
                for (int i = 0; i < rightArr.length; i++) {
                    String a = rightArr[i];
                    if (!a.equals("")) {
                        if (!a.equals("|")) Right.add(a);
                        if (a.equals("|") || i == rightArr.length - 1) {
                            StringBuilder res = new StringBuilder();
                            res.append(left).append(" ->");
                            for (String r : Right) {
                                res.append(" ").append(r);
                            }
                            out.write(res + "\r\n");
                            out.flush();
                            Right.clear();
                        }
                    }
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
